<?php
/**
 * User: Mark
 * Time: 13-6-24 下午2:10
 */
// 两个小时左右处理完成  12500 数据
require '../../common.inc.php';
// 获取一条老的数据
	// passenger
	$sql = "select * from xd_auto_passenger where isisis=0 order by id desc limit 1 ";
	// name
	$modelOld = $db->getOne($sql);
    if (!$modelOld) {exit(date("Y-m-d H:i:s"));}
	$sql = "select * from xd_auto where aid={$modelOld['aid']}";
	$carOld = $db->getOne($sql);
// 插入新的车款主表
		// year [2012款] => 239
		$year = handleYear($modelOld['market_time']);

		// 之前的车系ID
		$oldCarname = $carOld['name'];
			// 检查车系名称是否和现有的匹配, 如果匹配, 现在的ID就已经可以获取到
			// 现在有重复值正在检测中
		// 现在车系ID匹配中

		// aid  [车系id]
			// arbid
			// armid
			// arcid

		$ids = getMatchedids($oldCarname);
		$arbid = $ids[0];
		$armid = $ids[1];
		$arcid = $ids[2];

		// match acid 关联汽车的级别
		// acid     => 乘用/商用/房车   , 在之前分类中匹配现在的分级
		$acid = getAcid($carOld['cid']);
		// price 之前的价格 * 10000
		$price = formatPrice($modelOld['price']);

		// fuel_num  => enginetype
		$enginetype = getEngineType($modelOld['fuel_num']);
		//             emission
		$emission = getEmission($modelOld['emission_standard']);
		// transmission_type   =>  gearbox
		$gearbox = getGearbox($modelOld['transmission_type']);
		// status     =>      issale[在售/停售]
		$issale = $modelOld['status'];
		// auto_model
		// modeltype        =>  'passenger'
		$insert = "insert into xd_auto_model(
		name, year, arbid, armid, arcid, acid, price, enginetype, emission, gearbox, issale, modeltype,country, listorder
		) values (
		'{$modelOld['name']}',
		'{$year}',
		'{$arbid}',
		'{$armid}',
		'{$arcid}',
		'{$acid}',
		'{$price}',
		'{$enginetype}',
		'{$emission}',
		'{$gearbox}',
		'{$issale}',
		'passenger',
		0,
		20
		)";
		$db->query($insert);
		$amid = $db->insertId();

	// 插入新的车款附表
		// auto_model_passanger
	$newFields = PCache::read('fields-auto_model_passenger.php');
	$name = array();
	$name['length'] = $modelOld['long'];
	$name['amid'] = $amid;

	foreach($newFields as $k => $field) {
		if (isset($modelOld[$field['name']])) {
			if ($field['name'] == 'behind_mounting' || $field['name'] == 'forward_mounting') {
				$modelOld[$field['name']] = getHang($modelOld[$field['name']]);
			}
			$name[$field['name']] = $modelOld[$field['name']];
		}
	}


	$insertMf = Sql::insert('xd_auto_model_passenger', $name);
	var_dump($modelOld['id']);
	var_dump($modelOld['name']);
//	print_r($insertMf);
	var_dump($db->query($insertMf));
			// 对应参数
	$sql = "update xd_auto_passenger set isisis=1 where id={$modelOld['id']}";
	var_dump($db->query($sql));
echo '<meta http-equiv="refresh" content="0; url='.$_SERVER['PHP_SELF'].'" />';
function handleYear($old) {
	$years = PCache::read('autoCategory-year.php');
	foreach($years as $k => $v) {
		if ($v['name'] == $old) {
			return $k;
		}
	}
	return '0';
}

function getMatchedids($name) {
	global $db;
	$sql = "select arid, arrparentid from xd_auto_category where name='{$name}'";
	$ifo = $db->getOne($sql);
	if ($ifo) {
		$pids = explode(',', $ifo['arrparentid']);
		array_shift($pids);
		$pids[] = $ifo['arid'];
		return $pids;
	} else {
		return array(
			4274,4275,4276
		);
	}
}

function getAcid($oldAcid) {
	$oldLevel = PCache::read('oldAutoBrand.php');
	$oldLevelName = $oldLevel[$oldAcid]['catname'];
	$newLevel = PCache::read('autoCategory-level.php');
	foreach($newLevel as $k => $v) {
		if ($oldLevelName == $v['name']) {
			return $k;
		}
	}
	return 0;
}

function formatPrice($oldPrice) {
	return round($oldPrice*10000,0);
}

function getEngineType($tp) {
	//1=>"93#汽油",2=>"97号汽油",3=>"0号柴油",4=>"天然气",5=>"98#汽油"
	// 426 -> 汽油
	// 427 -> 柴油
	// 428 -> 天然气
	$arr = array(
		'1' => 426,
		'2' => 426,
		'3' => 427,
		'4' => 428,
		'5' => 426,
	);
	if (isset($arr[$tp])) {
		return $arr[$tp];
	}
	return 0;
}

function getGearbox($tp) {
	//  1=>"手动变速箱",2=>"自动变速箱",3=>"半自动变速箱",4=>"手自一体",5=>"无极变速",6=>"双离合变速",7=>"序列变速箱"
	$arr = array(
		'1' => 4314,
		'2' => 525,
		'3' => 524,
		'4' => 526,
		'5' => 527,
		'6' => 528,
		'7' => 4313,
	);
	if (isset($arr[$tp])) {
		return $arr[$tp];
	}
	return 0;
}

function getEmission($tp) {
	$arr = array(
		'欧IV' => 520,
		'国IV' => 520,
		'国4' => 520,
		'国V' => 521,
		'欧III' => 519,
		'欧V' => 521,
		'国III' => 519,
		'国3' => 519,
		'欧II' => 4326 ,
		'国II' => 4326 ,
	);
	if (isset($arr[$tp])) {
		return $arr[$tp];
	}
	return 0;
}

function getHang($tp) {
	$arr = array(
		'双叉臂式独立悬挂' => 4327,
		'多连杆式独立悬挂' => 4328,
		'麦弗逊式独立悬挂' => 4329,
		'双横臂式独立悬挂' => 4330,
		'整体桥式非独立悬挂' => 4331,
		'纵臂扭转梁式半独立悬挂' => 4332,
		'半拖式独立悬挂' => 4333,
		'全拖式独立悬挂' => 4334,
		'双摇臂式独立悬架' => 4335 ,
		'连杆支柱式独立悬挂' => 4336 ,
	);
	if (isset($arr[$tp])) {
		return $arr[$tp];
	}
	return 0;
}